package com.edu.controller;

import com.edu.Result;
import com.edu.StatusCode;
import com.edu.pojo.EduCurriculum;
import com.edu.pojo.EduDept;
import com.edu.pojo.EduTime;
import com.edu.service.EduCurService;
import com.edu.service.EduTeacherService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 课程管理
 *
 * @author YJF
 */
@RestController
@RequestMapping("/cur")
public class EduCurController {

    @Autowired
    private EduCurService eduCurService;

    @Autowired
    private EduTeacherService eduTeacherService;

    /**
     * 分页条件查找课程
     */
    @GetMapping("/list")
    public Result findByPage(HttpServletRequest request) {
        String page = request.getParameter("page");
        String size = request.getParameter("size");
        String curBy = request.getParameter("curBy");
        String curName = request.getParameter("curName");
        List<Integer> allTecId = eduTeacherService.findTecByName(curBy);
        PageInfo<EduCurriculum> curList = eduCurService.findByPage(page, size, curName, allTecId);
        return new Result(true, StatusCode.OK, "查询成功!", curList);
    }

    /**
     * 查询时间表
     */
    @GetMapping("/time")
    public Result findCurTime() {
        List<EduTime> timeList = eduCurService.findTime();
        return new Result(true, StatusCode.OK, "查找成功!", timeList);
    }

    /**
     * 添加课程信息
     */
    @PostMapping("/add")
    public Result addCur(@RequestBody EduCurriculum eduCurriculum) {
        int status = eduCurService.addCur(eduCurriculum);
        if (status == StatusCode.OK) {
            return new Result(true, StatusCode.OK, "添加成功!", eduCurriculum);
        }
        return new Result(false, StatusCode.ERROR, "添加失败!", eduCurriculum);
    }

    /**
     * 根据课程ID查找课程
     */
    @GetMapping("/one/{id}")
    public Result findOne(@PathVariable("id") Long id) {
        EduCurriculum eduCurriculum = eduCurService.findOne(id);
        return new Result(true, StatusCode.OK, "查询成功!", eduCurriculum);
    }

    /**
     * 根据课程ID修改课程信息
     */
    @PutMapping("/update")
    public Result updateCur(@RequestBody EduCurriculum eduCurriculum) {
        int status = eduCurService.updateCur(eduCurriculum);
        if (status == StatusCode.OK) {
            return new Result(true, StatusCode.OK, "修改成功!", eduCurriculum);
        }
        return new Result(false, StatusCode.ERROR, "修改失败!", eduCurriculum);
    }

    /**
     * 根据课程ID删除课程
     */
    @DeleteMapping("/del/{id}")
    public Result delCur(@PathVariable("id") Long id) {
        int status = eduCurService.delCur(id);
        if (status == StatusCode.OK) {
            return new Result(true, StatusCode.OK, "删除成功!", id);
        }
        return new Result(true, StatusCode.ERROR, "删除失败!", id);
    }

    /**
     * 根据班级ID和课程ID添加课程信息
     */
    @PostMapping("/addCls/{id}")
    public Result addClsById(@PathVariable("id") Integer id, @RequestBody EduCurriculum eduCurriculum) {
        Long curId = eduCurriculum.getCurId();
        // 向课程和班级中间表添加消息,班级必修
        int state = eduCurService.findCurAndCls(id, curId);
        if (state == StatusCode.REP_ERROR) {
            return new Result(false, StatusCode.REP_ERROR, "重复添加!");
        } else if (state == StatusCode.NOT_FIND) {
            return new Result(false, StatusCode.NOT_FIND, "未找到班级!");
        }
        EduDept eduDept = eduCurService.addCurAndCls(id, curId);
        return new Result(true, StatusCode.OK, "查询成功!", eduDept);
    }

    /**
     * 根据班级ID和课程ID删除中间表edu_cur_cls相关信息
     */
    @DeleteMapping("/delCls/{id}/{curId}")
    public Result delCurAndCls(@PathVariable("id") Integer id, @PathVariable("curId") Long curId) {
        eduCurService.delCurAndCls(id, curId);
        return new Result(true, StatusCode.OK, "删除成功!");
    }
}
